using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetDocumentConceptTopics : UserControl, DemoControl
    {
        public RichTextBox oTextBox;

        public ctrlGetDocumentConceptTopics(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;

            // Set Process button state
            EnableProcessButton();
        }

        public void processText()
        {
            gridResults.Rows.Clear();

            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                List<SalienceTopic> oResults = SalienceDemo.oSalienceSession.GetConceptDefinedTopics();

                if (oResults.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceTopic oTopic in oResults)
                    {
                        gridResults.Rows.Add();
                        gridResults.Rows[nCount].Cells[0].Value = oTopic.sTopic;
                        gridResults.Rows[nCount].Cells[1].Value = oTopic.fScore;
                        gridResults.Rows[nCount].Cells[2].Value = oTopic.fSentiment;
                        gridResults.Rows[nCount].Cells[3].Value = oTopic.nHits;
                        nCount++;
                    }
                    btnExplain.Enabled = true;
                    gridResults.AutoResizeRows();
                }
                else
                {
                    MessageBox.Show("No concept-defined topics were retrieved for this content.", "GetConceptDefinedTopics()");
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        private void getDocumentTags_VisibleChanged(object sender, EventArgs e)
        {
            gridResults.Rows.Clear();
            btnExplain.Enabled = false;
        }

        private void btnExplain_Click(object sender, EventArgs e)
        {
            string explain = SalienceDemo.oSalienceSession.ExplainConceptMatches();
            SalienceEngineDemo.Dialogs.ExplainConcepts dlgExplain = new Dialogs.ExplainConcepts(explain);
            dlgExplain.ShowDialog();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.DOCUMENTS);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.gridResults.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridResults.AutoResizeRows();
        }

        public void EnableProcessButton()
        {
            // Process button enabled by existence of concept topic file and existence of text to process
            btnProcess.Enabled = ((System.IO.File.Exists(SalienceDemo.oConfig.activeSettings().settings["DefaultConceptTopicFile"])) &&
                                    (oTextBox.Text.Trim().Length > 0));
        }

        #endregion

    }
}
